package edu.msse.signup.ui;

import java.util.List;

import javax.faces.context.FacesContext;


import edu.msse.signup.domain.Users;
import edu.msse.signup.service.UsersService;;

public class updateUserHandler {
	private String name;
	private String password;
	private String phoneNumber;
	private String emailAddress;
	private UsersService userService;
	private Users currentUser;
	
	public updateUserHandler(UsersService usersService)
	{
		this.userService=usersService;
		loginHandler l=(loginHandler)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("loginHandler");
		List<Users> u=this.userService.findUsersByEmailAddress(l.getUsername());
		currentUser=u.get(0);
		setEmailAddress(currentUser.getEmailAddress());
		setPassword(currentUser.getPassword());
		setName(currentUser.getName());
		setPhoneNumber(currentUser.getPhoneNumber());
		
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	public String getEmailAddress() {
		return emailAddress;
	}

	public void setEmailAddress(String emailAddress) {
		this.emailAddress = emailAddress;
	}
	
	public String submit() throws Exception{
		try{
		
		currentUser.setEmailAddress(getEmailAddress());
		currentUser.setName(getName());
		currentUser.setPassword(getPassword());
		currentUser.setPhoneNumber(getPhoneNumber());
		userService.updateUser(currentUser);
		}
		catch(Exception E)
		{
			E.printStackTrace();
			throw E;
			
		}
		return "success";
	}

}
